package com.define.commons.dao.autoconfig;

import com.define.commons.dao.register.DaoBeanDefinitionRegister;
import com.gitee.fastmybatis.core.FastmybatisConfig;
import com.gitee.fastmybatis.core.support.DateFillInsert;
import com.gitee.fastmybatis.core.support.DateFillUpdate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.ArrayList;

/**
 * Dao层项目的自动配置
 * @Author xiewenlong
 * @Date 2018/11/16 14:56
 */
@Slf4j
@Configuration
@Import(DaoBeanDefinitionRegister.class)
@EnableConfigurationProperties(DaoProperties.class)
public class DaoAutoConfig {

    @Bean
    @ConditionalOnMissingBean
    public FastmybatisConfig fastmybatisConfig(DaoProperties properties){
        FastmybatisConfig config = new FastmybatisConfig();
        config.setFills(new ArrayList(){{
            add(new DateFillInsert(properties.getDateFillInsert()));
            add(new DateFillUpdate(properties.getDateFillUpdate()));
        }});
        config.setLogicDeleteValue(properties.getLogicDeleteValue());
        config.setLogicNotDeleteValue(properties.getLogicNotDeleteValue());
        log.info("commons-dao autoconfig FastmybatisConfig: {}", properties);
        return config;
    }

    @Bean
    @ConditionalOnMissingBean
    public org.apache.ibatis.session.Configuration configuration(){
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(true);
        configuration.setAggressiveLazyLoading(false);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setUseGeneratedKeys(true);
        StringBuilder sb = new StringBuilder("[")
                .append("AggressiveLazyLoading = true, ")
                .append("LazyLoadingEnabled = true, ")
                .append("MapUnderscoreToCamelCase = true, ")
                .append("UseGeneratedKeys = true]");
        log.info("commons-dao autoconfig MybatisProperties: {}", sb.toString());
        return configuration;
    }

/*    @Bean
    @ConditionalOnBean(com.gitee.fastmybatis.spring.boot.autoconfigure.MybatisProperties.class)
    public com.gitee.fastmybatis.spring.boot.autoconfigure.MybatisProperties mybatisProperties(com.gitee.fastmybatis.spring.boot.autoconfigure.MybatisProperties mybatisProperties){
        System.out.println(mybatisProperties);
        return mybatisProperties;
    }*/

/*    @Bean
    @ConditionalOnBean({org.apache.ibatis.session.Configuration.class})
    @ConditionalOnMissingBean
    public MybatisProperties mybatisProperties(org.apache.ibatis.session.Configuration configuration){
        System.out.println("MybatisProperties...");
        MybatisProperties mybatisProperties = new MybatisProperties();
        mybatisProperties.setConfiguration(configuration());
        mybatisProperties.setTypeAliasesPackage("com.define.**.domain");
        mybatisProperties.setMapperLocations(new String[]{"classpath:mapper/**.xml"});
        StringBuilder sb = new StringBuilder("[");
        sb.append("TypeAliasesPackage = com.define.**.domain, ")
                .append("MapperLocations = classpath:mapper/**.xml]");
        log.info("commons-dao autoconfig MybatisProperties: {}", sb.toString());
        return mybatisProperties;
    }*/

    @Bean
    @ConditionalOnMissingBean
    public JpaProperties jpaProperties(DaoProperties properties){
        JpaProperties jpaProperties = new JpaProperties();
        jpaProperties.setShowSql(properties.isShowSql());
        jpaProperties.setDatabasePlatform(properties.getDatabasePlatform());
        jpaProperties.getHibernate().setDdlAuto(properties.getDdlAuto());
        log.info("commons-dao autoconfig JpaProperties: {}", properties);
        return jpaProperties;
    }

}
